﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using System.Xml.Serialization;
using RaisingForce.Raise.Hardware.Rules;
using RaisingForce.Raise.Measures;

namespace RaisingForce.Raise.Hardware
{
    [Serializable]
    [XmlType("HardwareOptions")]
    sealed public class RobotHardwareOptions :
        IRobotHardwareOptions
    {
        #region Constructors

        public RobotHardwareOptions(IRobotHardwareOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            Contract.EndContractBlock();
            this.comPort = options.ComPort;
            this.obstacleDistance = options.ObstacleDistance;
            this.wheelRunDistance = options.WheelRunDistance;
            this.wheelBalance = options.WheelBalance;
            this.leftTurnTacho = options.LeftTurnTacho;
            this.rightTurnTacho = options.RightTurnTacho;
            this.supposedPower = options.SupposedPower;
            this.obstacleDetectionTime = options.ObstacleDetectionTime;
            this.wheelRunTime = options.WheelRunTime;
            this.leftTurnTime = options.LeftTurnTime;
            this.rightTurnTime = options.RightTurnTime;
        }

        public RobotHardwareOptions(
            byte comPort,
            Distance obstacleDistance,
            Distance wheelRunDistance,
            float wheelBalance,
            ushort leftTurnTacho,
            ushort rightTurnTacho,
            sbyte supposedPower,
            Time obstacleDetectionTime,
            Time wheelRunTime,
            Time leftTurnTime,
            Time rightTurnTime)
        {
            if (!RobotHardwareOptionsRules.ComPortIsValid(comPort))
            {
                throw new ArgumentException("comPort");
            }
            if (!RobotHardwareOptionsRules.DistanceIsValid(obstacleDistance))
            {
                throw new ArgumentException("obstacleDistance");
            }
            if (!RobotHardwareOptionsRules.DistanceIsValid(wheelRunDistance))
            {
                throw new ArgumentException("wheelRunDistance");
            }
            if (!RobotHardwareOptionsRules.BalanceIsValid(wheelBalance))
            {
                throw new ArgumentException("wheelBalance");
            }
            if (!RobotHardwareOptionsRules.TachoIsValid(leftTurnTacho))
            {
                throw new ArgumentException("leftTurnTacho");
            }
            if (!RobotHardwareOptionsRules.TachoIsValid(rightTurnTacho))
            {
                throw new ArgumentException("rightTurnTacho");
            }
            if (!RobotHardwareOptionsRules.PowerIsValid(supposedPower))
            {
                throw new ArgumentException("supposedPower");
            }
            if (!RobotHardwareOptionsRules.TimeIsValid(obstacleDetectionTime))
            {
                throw new ArgumentException("obstacleDetectionTime");
            }
            if (!RobotHardwareOptionsRules.TimeIsValid(wheelRunTime))
            {
                throw new ArgumentException("wheelRunTime");
            }
            if (!RobotHardwareOptionsRules.TimeIsValid(leftTurnTime))
            {
                throw new ArgumentException("leftTurnTime");
            }
            if (!RobotHardwareOptionsRules.TimeIsValid(rightTurnTime))
            {
                throw new ArgumentException("rightTurnTime");
            }
            Contract.EndContractBlock();
            this.comPort = comPort;
            this.obstacleDistance = obstacleDistance;
            this.wheelRunDistance = wheelRunDistance;
            this.wheelBalance = wheelBalance;
            this.leftTurnTacho = leftTurnTacho;
            this.rightTurnTacho = rightTurnTacho;
            this.supposedPower = supposedPower;
            this.obstacleDetectionTime = obstacleDetectionTime;
            this.wheelRunTime = wheelRunTime;
            this.leftTurnTime = leftTurnTime;
            this.rightTurnTime = rightTurnTime;
        }

        public RobotHardwareOptions()
            : this(RobotHardwareOptions.Default)
        { }

        #endregion

        #region Private Declarations

        private byte comPort;

        private Distance obstacleDistance;

        private Distance wheelRunDistance;

        private float wheelBalance;

        private ushort leftTurnTacho;

        private ushort rightTurnTacho;

        private sbyte supposedPower;

        private Time obstacleDetectionTime;

        private Time wheelRunTime;

        private Time leftTurnTime;

        private Time rightTurnTime;

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(RobotHardwareOptionsRules.ComPortIsValid(this.comPort));
            Contract.Invariant(RobotHardwareOptionsRules.DistanceIsValid(this.obstacleDistance));
            Contract.Invariant(RobotHardwareOptionsRules.DistanceIsValid(this.wheelRunDistance));
            Contract.Invariant(RobotHardwareOptionsRules.BalanceIsValid(this.wheelBalance));
            Contract.Invariant(RobotHardwareOptionsRules.TachoIsValid(this.leftTurnTacho));
            Contract.Invariant(RobotHardwareOptionsRules.TachoIsValid(this.rightTurnTacho));
            Contract.Invariant(RobotHardwareOptionsRules.PowerIsValid(this.supposedPower));
            Contract.Invariant(RobotHardwareOptionsRules.TimeIsValid(this.obstacleDetectionTime));
            Contract.Invariant(RobotHardwareOptionsRules.TimeIsValid(this.wheelRunTime));
            Contract.Invariant(RobotHardwareOptionsRules.TimeIsValid(this.leftTurnTime));
            Contract.Invariant(RobotHardwareOptionsRules.TimeIsValid(this.rightTurnTime));
        }

        #endregion

        #region Public Methods

        public IRobotHardwareOptions AsReadOnly()
        {
            Contract.Ensures(Contract.Result<IRobotHardwareOptions>() != null);
            return new ReadOnlyRobotHardwareOptions(this);
        }

        #endregion

        #region Base Overrides

        public override bool Equals(object obj)
        {
            return obj is IRobotHardwareOptions ?
                this.Equals((IRobotHardwareOptions)obj) :
                false;
        }

        public override int GetHashCode()
        {
            return IRobotHardwareOptionsExtensions.GetHashCode(this);
        }

        #endregion

        #region Static Declarations

        public static readonly IRobotHardwareOptions Default =
            new RobotHardwareOptions(
                comPort: 1,
                obstacleDistance: Distance.DefineInCentimetre(25),
                wheelRunDistance: Distance.DefineInCentimetre(10.5),
                wheelBalance: 1,
                leftTurnTacho: 1850,
                rightTurnTacho: 1850,
                supposedPower: 50,
                obstacleDetectionTime: Time.DefineInMilliseconds(500),
                wheelRunTime: Time.DefineInMilliseconds(500),
                leftTurnTime: Time.DefineInMilliseconds(1000),
                rightTurnTime: Time.DefineInMilliseconds(1000))
            .AsReadOnly();

        #endregion

        #region IRobotHardwareOptions Members

        [XmlElement]
        public byte ComPort
        {
            get
            {
                return this.comPort;
            }
            set
            {
                if (!RobotHardwareOptionsRules.ComPortIsValid(value))
                {
                    throw new ArgumentException("ComPort");
                }
                Contract.EndContractBlock();
                this.comPort = value;
            }
        }

        [XmlElement]
        public Distance ObstacleDistance
        {
            get
            {
                return this.obstacleDistance;
            }
            set
            {
                if (!RobotHardwareOptionsRules.DistanceIsValid(value))
                {
                    throw new ArgumentException("ObstacleDistance");
                }
                Contract.EndContractBlock();
                this.obstacleDistance = value;
            }
        }

        [XmlElement]
        public Distance WheelRunDistance
        {
            get
            {
                return this.wheelRunDistance;
            }
            set
            {
                if (!RobotHardwareOptionsRules.DistanceIsValid(value))
                {
                    throw new ArgumentException("WheelRunDistance");
                }
                Contract.EndContractBlock();
                this.wheelRunDistance = value;
            }
        }

        [XmlElement]
        public float WheelBalance
        {
            get
            {
                return this.wheelBalance;
            }
            set
            {
                if (!RobotHardwareOptionsRules.BalanceIsValid(value))
                {
                    throw new ArgumentException("WheelBalance");
                }
                Contract.EndContractBlock();
                this.wheelBalance = value;
            }
        }

        [XmlElement]
        public ushort LeftTurnTacho
        {
            get
            {
                return this.leftTurnTacho;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TachoIsValid(value))
                {
                    throw new ArgumentException("LeftTurnTacho");
                }
                Contract.EndContractBlock();
                this.leftTurnTacho = value;
            }
        }

        [XmlElement]
        public ushort RightTurnTacho
        {
            get
            {
                return this.rightTurnTacho;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TachoIsValid(value))
                {
                    throw new ArgumentException("RightTurnTacho");
                }
                Contract.EndContractBlock();
                this.rightTurnTacho = value;
            }
        }

        [XmlElement]
        public sbyte SupposedPower
        {
            get
            {
                return this.supposedPower;
            }
            set
            {
                if (!RobotHardwareOptionsRules.PowerIsValid(value))
                {
                    throw new ArgumentException("SupposedPower");
                }
                Contract.EndContractBlock();
                this.supposedPower = value;
            }
        }

        [XmlElement]
        public Time ObstacleDetectionTime
        {
            get
            {
                return this.obstacleDetectionTime;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TimeIsValid(value))
                {
                    throw new ArgumentException("ObstacleDetectionTime");
                }
                Contract.EndContractBlock();
                this.obstacleDetectionTime = value;
            }
        }

        [XmlElement]
        public Time WheelRunTime
        {
            get
            {
                return this.wheelRunTime;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TimeIsValid(value))
                {
                    throw new ArgumentException("WheelRunTime");
                }
                Contract.EndContractBlock();
                this.wheelRunTime = value;
            }
        }

        [XmlElement]
        public Time LeftTurnTime
        {
            get
            {
                return this.leftTurnTime;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TimeIsValid(value))
                {
                    throw new ArgumentException("LeftTurnTime");
                }
                Contract.EndContractBlock();
                this.leftTurnTime = value;
            }
        }

        [XmlElement]
        public Time RightTurnTime
        {
            get
            {
                return this.rightTurnTime;
            }
            set
            {
                if (!RobotHardwareOptionsRules.TimeIsValid(value))
                {
                    throw new ArgumentException("RightTurnTime");
                }
                Contract.EndContractBlock();
                this.rightTurnTime = value;
            }
        }

        public bool Equals(IRobotHardwareOptions other)
        {
            return IRobotHardwareOptionsExtensions.Equals(this, other);
        }

        #endregion
    }
}
